<?php
namespace Common\Controller;
use Common\Controller\CommonController;
use Admin\Model\AdminModel;

class AdminBaseController extends CommonController
{
    protected $_admin = array();

    protected function _initialize() {

        parent::_initialize();

        if (MODULE_NAME . CONTROLLER_NAME == 'AdminLogin') {
            return false;
            exit();
        }

        $this->_admin = session('admin');
        $admin_id = x_get_admin_id();

        //未登陆，不允许直接访问
        if (empty($admin_id)) {
            $this->error('还没有登录,正在跳转到登录页', U('Admin/Login/index'));
            exit();
        }

        if (!$this->_check_access($admin_id)) {
            $this->error('您没有访问权限', '/Admin/Index/main');
            exit();
        }



        $this->assign('admin', $this->_admin);
        $this->assign('__MENU__', $this->initMenus());
        $this->assign('__NAVIGATION__', $this->_getNavigation());


    }

    /**
     * 获取经过权限验证后的菜单
     */
    final public function initMenus() {
        return D("Admin/Menu")->menusAll();
    }

    /**
     * 权限检测
     */
    private function _check_access($admin_id) {

        if (empty($admin_id)) {
            return false;
        }

        if ($admin_id == AdminModel::DEFAULT_ADMIN) {
            return true; //管理员允许访问任何页面
        }

        $rule = MODULE_NAME . CONTROLLER_NAME . ACTION_NAME;
        //已登录,不需要验证的权限
        $not_check = array('AdminIndexindex', 'AdminIndexmain');
        if (!in_array($rule, $not_check)) {
            return x_check_rule($admin_id);
        } else {
            return true;
        }
    }

    /**
     *  排序 排序字段为：默认sort字段或者自定义字段
     */
    protected function _updateListSort($model, $field = '') {
        if (!is_object($model)) return false;
        $field = $field ? : 'sort';
        $pk    = $model->getPk(); //获取主键名称
        $sorts = I('post.sorts');
        foreach ($sorts as $key => $val) {
            $data = array(
                $pk     => intval($key),
                $field => intval($val)
            );
            $model->save($data);
        }
        return true;
    }

    /*
     * 自定义后台模板路径
     * */
    public function config_tmpl_path() {
        $tmpl_path = C("X_ADMIN_TMPL_PATH");  //后台模板路径
        $theme     = C('X_ADMIN_DEFAULT_THEME'); //后台模板名称
        define('TMPL_PATH', $tmpl_path . $theme . '/');
        C("TMPL_PARSE_STRING.__ADMIN_IMG__",  __ROOT__. '/' . TMPL_PATH . 'Public/img');
        C("TMPL_PARSE_STRING.__ADMIN_CSS__",  __ROOT__. '/' . TMPL_PATH . 'Public/css');
        C("TMPL_PARSE_STRING.__ADMIN_JS__",  __ROOT__. '/' . TMPL_PATH . 'Public/js');
        C("TMPL_ACTION_SUCCESS", '../Public/' . C("X_ADMIN_TMPL_ACTION_SUCCESS"));
        C("TMPL_ACTION_ERROR", '../Public/' . C("X_ADMIN_TMPL_ACTION_ERROR"));
    }

    private function _getNavigation() {
        $navigations = array();

        $_p = I('get._p');
        if (empty($_p)) {
            return $navigations;
        }

        $navigation_ids = explode('-', $_p);
        $menus = D("Admin/Menu")->fetchAll();
        if(is_array($navigation_ids) && !empty($navigation_ids)){
            foreach ($navigation_ids as $menu_id) {
                array_push($navigations,$menus[$menu_id]['menu_name']);
            }
        }

        return $navigations;
    }
}